<template>
  <div>
  </div>
</template>

<script>
import axios  from "axios";
import { mapState } from "vuex"
export default {
  name: "videoRoom",
  data() {
    return {
      appID: 1234591,
      userID: "",
      tryCount: 1,
    }
  },
  mounted() {
    this.userID = this.globalData.cifNo || '1q2w3e4r5t';
    this.autoLogin();
  },
  methods:{

    autoLogin() {
      const { appID, userID } = this;
      this.getLoginToken().then(res => {
        if (!res) {
          console.log(`---获取token失败，重新获取 ${this.tryCount } 次---`, res);
          if (this.tryCount < 20) {
            setTimeout(() => {
              this.autoLogin();
            }, 2000)
          }
        debugger
          if (this.tryCount >= 20) {
            this.$toast.clear();
            this.$toast.fail({ duration: 0, message: '获取token失败, 进入视频失败！' });
          }
          this.tryCount++
          return false;
        }
        this.$toast.clear();
        console.log('获取token成功', res);
        let token = res.login_token

        this.token = token
        let params = {
          "appID": appID,
          "channel": this.globalData.roomNum,
          "appSign": "12345678901234567890123456789012",
          "uid": `miniapp-device-${userID}`,
          "videoDispatchUrl": "https://malla.leagpoint.com",
          // "videoDispatchUrl": "https://rfs.huajifen.com",
          // "videoDispatchUrl": env.develop.videoUrl,
          "userID": userID,
          "userName": `miniapp-user-${userID}`,
          "token": token,
          "role": "2",
          "reserveId": this.globalData.reserveId,
          "bankCode": this.globalData.bankCode,
          "access_token": this.globalData.access_token,
          "sessionId": this.globalData.sessionId,
        }
        window.mbankVideoChat.startVideoChat(params)
        this.$toast.loading({ duration: 0, message: '正在连接视频请稍等！' });

        console.log(window.mbankVideoChat.startVideoChat, "请求了视频开始方法");
        console.log(params, "请求了参数");
      }).catch(err => {
        console.log('---获取token失败---', err);
        // this.autoLogin();
      });
    },
    async getLoginToken() {
      const { appID, userID } = this;
      try {
        const params = {
          url: "https://malla.leagpoint.com/logintoken",
          // url: env.develop.videoUrl + '/logintoken',
          // url: "https://rfs.huajifen.com/logintoken",
          // url: loginServer,
          method: 'POST',
          data: {
            seq: 1,
            timestamp: Math.ceil(new Date().getTime() / 1000),
            app_id: appID,
            user_id: userID,
            user_name: `miniapp-user-${userID}`,
            queue_role: 10,
            room_role: 0,
            net_type: 2,
            device_id: `miniapp-device-${userID}`,
          }
        }
        console.log('获取token请求参数：', params);
        const result = await axios(params);
        if (result.status != 200) {
          return false;
        } else {
          return result.data;
        }
      } catch (e) {
        console.log("进入视频失败：" + e)
        this.$toast.fail({ duration: 0, message: '进入视频失败: ' + e });
      }
    },
  },
  computed: {
    ...mapState({
      globalData: state => state.globalData
    })
  },
}
</script>